package examples.usingjdo;

import static org.junit.Assert.assertEquals;
import imai78gae.heya.model.UserInfo;
import imai78gae.heya.util.PMF;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.junit.Test;

import test.BaseTestCase;

public class JdoUsingSample extends BaseTestCase {

	@SuppressWarnings("unchecked")
	@Test
	public void case1_Queryを使う() {
		//
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		tx.begin();
		try {
			UserInfo user = new UserInfo("aaa", "bbb", "ccc");
			pm.makePersistent(user);
			tx.commit();
			Query query = pm.newQuery(UserInfo.class);
			List<UserInfo> result = (List<UserInfo>) query.execute();
			System.out.println("size[0]=" + result.size());
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
		//
		pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserInfo.class);
		List<UserInfo> result = (List<UserInfo>) query.execute();
		System.out.println("size[1]=" + result.size());
		pm.close();
	}

	@SuppressWarnings("unchecked")
	@Test
	public void case2_Modifyする() {
		//
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		tx.begin();
		try {
			UserInfo user = new UserInfo("aaa", "bbb", "ccc");
			pm.makePersistent(user);
			tx.commit();
			Query query = pm.newQuery(UserInfo.class);
			List<UserInfo> result = (List<UserInfo>) query.execute();
			System.out.println("size[0]=" + result.size());
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
		//
		pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(UserInfo.class);
		List<UserInfo> result = (List<UserInfo>) query.execute();
		System.out.println("size[1]=" + result.size());
		pm.close();
		//
		pm = PMF.get().getPersistenceManager();
		tx = pm.currentTransaction();
		tx.begin();
		try {
			UserInfo user = result.get(0);
			user.setAccountName("ddd");
			pm.makePersistent(user);
			tx.commit();
			query = pm.newQuery(UserInfo.class);
			result = (List<UserInfo>) query.execute();
			UserInfo user2 = result.get(0);
			assertEquals("aaa", user2.getLoginId());
			assertEquals("ddd", user2.getAccountName());
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Test
	public void case3_Listを含んだBeanの保存と取得() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		tx.begin();
		try {
			TestBean bean = new TestBean();
			bean.setBeanDesc("aaaaa");
			List<String> list = new ArrayList<String>();
			list.add("aaa");
			list.add("bbb");
			bean.setBeanList(list);
			pm.makePersistent(bean);
			tx.commit();
		} finally {
			if (tx.isActive())
				tx.rollback();
			pm.close();
		}
		pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(TestBean.class);
		List<TestBean> result = (List<TestBean>) query.execute();
		System.out.println("result.size()=" + result.size());
		TestBean resultBean = result.get(0);
		System.out.println("resultBean.getBeanDesc()=" + resultBean.getBeanDesc());
		System.out.println("resultBean.getBeanList().size()=" + resultBean.getBeanList().size());
	}

}
